﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATM
{
    public class InputWithdrawAmountState : ATMState
    {
        public ATM atm;

        public InputWithdrawAmountState(ATM atm)
        {
            this.atm = atm;
        }

        public void pressPowerButton()
        {
            return;
        }

        public void pressTakeCashButton()
        {
            return;
        }

        public void pressInsertEnvelopButton()
        {
            return;
        }

        public void pressEnterButton()
        {
            if (this.atm.keypadScreen.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("请输入数字后按Enter键确认!");
                return;
            }

            this.atm.commandString = this.atm.keypadScreen.Text;
            this.atm.commandNumber = Convert.ToInt32(this.atm.commandString);
            this.atm.keypadScreen.Text = "";

            this.atm.withdrawal.setAccountID(this.atm.currentAccountID);
            this.atm.withdrawal.setAmount(this.atm.commandNumber);

            if (this.atm.withdrawal.amount % 100 != 0)
            {
                System.Windows.Forms.MessageBox.Show("此ATM机只能提供面额为100的纸币，请重新输入");
                return;
            }

            if (this.atm.bankDatabase.balanceInquiry(this.atm.currentAccountID) < this.atm.withdrawal.amount)
            {
                System.Windows.Forms.MessageBox.Show("您的账户余额不足，请重新选择金额");
                this.atm.setState(this.atm.inputWithdrawAmountState);
                this.atm.screen.Text = "请输入取款金额：";
                return;
            }

            if (this.atm.bankDatabase.balanceInquiry(this.atm.currentAccountID) > this.atm.moneyAmount)
            {
                System.Windows.Forms.MessageBox.Show("ATM机内存款不足，请重新选择金额");
                this.atm.setState(this.atm.inputWithdrawAmountState);
                this.atm.screen.Text = "请输入取款金额：";
                return;
            }

            this.atm.withdrawal.withdrawMoney(this.atm.belongTo,this.atm.bankDatabase);
            this.atm.setState(this.atm.takeMoneyState);
            this.atm.cashDispenser.Text = "[-------------]";
            this.atm.takeCashButton.Enabled = true;
            this.atm.screen.Text = "请提取现金";
            this.atm.receipt.receiptDetails = "账号："+this.atm.currentAccountID+"\r\n";
            this.atm.receipt.receiptDetails += "交易类型：取款" + "\r\n";
            this.atm.receipt.receiptDetails += "取款金额:" + this.atm.withdrawal.amount+"\r\n";
            this.atm.receipt.receiptDetails += "取款手续费:" + this.atm.withdrawal.commission + "\r\n";
            this.atm.receipt.receiptDetails += "账户余额:" + this.atm.bankDatabase.balanceInquiry(this.atm.currentAccountID);
            this.atm.enterButton.Enabled = false;
            this.atm.log.addLog(this.atm.receipt.receiptDetails);
            return;
        }
    }
}
